package com.demo.webapp.jcaptcha;

import java.awt.image.BufferedImage;
import java.util.Locale;

import com.octo.captcha.CaptchaException;
import com.octo.captcha.CaptchaQuestionHelper;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.image.ImageCaptcha;
import com.octo.captcha.image.gimpy.GimpyFactory;

/**
 * IgnoreCaseGimpy对象生成的工厂对象.
 * 
 * @author ryuu.kk
 */
public class IgnoreCaseGimpyFactory extends GimpyFactory {

	/**
	 * 构造函数
	 * @param generator 验证字生成器
	 * @param word2image 文字图片转换器
	 */
	public IgnoreCaseGimpyFactory(WordGenerator generator, WordToImage word2image) {
		super(generator, word2image);
	}

	/**
	 * 取得图片验证的Bean对象
	 * 
	 * @param locale Locale
	 * @return ImageCaptcha 
	 */
	public ImageCaptcha getImageCaptcha(Locale locale) {
		Integer wordLength = getRandomLength();

		String word = getWordGenerator().getWord(wordLength, locale);

		BufferedImage image = null;
		try {
			image = getWordToImage().getImage(word);
		} catch (Throwable e) {
			throw new CaptchaException(e);
		}

		ImageCaptcha captcha = new IgnoreCaseGimpy(CaptchaQuestionHelper.getQuestion(
				locale, BUNDLE_QUESTION_KEY), image, word);

		return captcha;
	}
}
